package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.entity.ApAuthor;
import com.heima.article.mapper.ApAuthorMapper;
import com.heima.article.service.IApAuthorService;
import com.heima.common.dto.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * APP文章作者信息表  服务实现类
 */
@Service
public class ApAuthorServiceImpl extends ServiceImpl<ApAuthorMapper, ApAuthor> implements IApAuthorService {

    @Override
    public ResponseResult<ApAuthor> saveApAuthor(ApAuthor entity) {
        // 判断作者是否存在
        // 根据 user_id 查询
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getUserId,entity.getUserId());
        int count = this.count(queryWrapper);
        // 如果存在，直接返回
        if (count == 0){
            // 设置时间
            entity.setCreatedTime(new Date());
            // 否则，新增作者，然后返回
            this.save(entity);
        }
        return ResponseResult.okResult(entity);
    }

    @Override
    public boolean save(ApAuthor entity) {
        return super.save(entity);
    }
}
